package com.ciip.cloud.core.ycg.service.impl;


import com.cool.model.common.ResultStatusCode;
import com.ciip.cloud.core.common.vo.ResponseMessage;

import com.ciip.cloud.core.ycg.model.TemplateModel;
import com.ciip.cloud.core.ycg.repository.TemplateModelRepository;

import org.springframework.beans.BeanUtils;
import com.github.wenhao.jpa.Specifications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Title:  TemplateModelServiceImpl
 * Description: 请填写描述信息
 * @author yls
 * @created  2020年03月06日 10:28:20 星期五
 */
@Service
public class TemplateModelServiceImpl implements  TemplateModelService{

    @Autowired
    private TemplateModelRepository templateModelRepository;

    @Override
    public ResponseMessage detail(Long id) {
        //根据所属id，查询信息
        Specification<TemplateModel> specification = Specifications.<TemplateModel>and()
                .eq("id", id)
               // .eq("delFlag", CommonConstant.DelFlag.NO)
                .build();
        Optional<TemplateModel> optional = templateModelRepository.findOne(specification);
        //信息不存在
        if (!optional.isPresent()) {
            return ResultUtil.error(ResultStatusCode.MESSAGE_NOT_EXIST.getCode(), ResultStatusCode.MESSAGE_NOT_EXIST.getMsg());
        }
        return ResultUtil.success(optional.get());
    }

    @Override
    public ResponseMessage list() {
        // 方案一 、添加按创建时间降序查询
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        //未删除
        Specification<TemplateModel> specification = Specifications.<TemplateModel>and()
                //.eq("delFlag", CommonConstant.DelFlag.NO)
                .build();
        List<TemplateModel> list = templateModelRepository.findAll(specification, sort);
        return ResultUtil.success(list);
    }

    @Override
    public ResponseMessage pageList(Integer pageNumber, Integer pageSize, HttpServletRequest request) {
        //未删除
        Specification<TemplateModel> specification = Specifications.<TemplateModel>and()
               // .eq("delFlag", CommonConstant.DelFlag.NO)
                .build();
        Page<TemplateModel> pageList = templateModelRepository.findAll(specification, PageRequest.of(pageNumber - 1, pageSize, Sort.Direction.DESC, "createDate"));
        return ResultUtil.success(pageList);
    }

    @Override
    public ResponseMessage save(TemplateModel  templateModel, HttpServletRequest request) {
             //创建更新时间
             templateModel.setCreateDate(new Date());
             templateModel.setModifyDate(new Date());
             templateModelRepository.save( templateModel);
             return ResultUtil.success(ResultStatusCode.SAVE_SUCCESS.getCode(),ResultStatusCode.SAVE_SUCCESS.getMsg());
    }

    @Override
    public ResponseMessage update(TemplateModel  templateModel, HttpServletRequest request) {
         if( templateModel.getId()==null){
            return ResultUtil.error(ResultStatusCode.PARAM_ERROR.getCode(), ResultStatusCode.PARAM_ERROR.getMsg());
         }
        //根据所属id，查询信息
         Specification<TemplateModel> specification = Specifications.<TemplateModel>and()
                 .eq("id",  templateModel.getId())
                 .build();
         Optional<TemplateModel> optional = templateModelRepository.findOne(specification);
         //信息不存在
         if (!optional.isPresent()) {
             return ResultUtil.error(ResultStatusCode.MESSAGE_NOT_EXIST.getCode(), ResultStatusCode.MESSAGE_NOT_EXIST.getMsg());
         }

         TemplateModel currentTemplateModel=optional.get();
          BeanUtils.copyProperties( templateModel, currentTemplateModel);
          //创建更新时间
         currentTemplateModel.setModifyDate(new Date());
         templateModelRepository.save(currentTemplateModel);
         return ResultUtil.success(ResultStatusCode.UPDATE_SUCCESS.getCode(),ResultStatusCode.UPDATE_SUCCESS.getMsg());
    }

    @Override
    public ResponseMessage deleteById(Long id) {
        //根据所属id，查询信息
        Specification<TemplateModel> specification = Specifications.<TemplateModel>and()
                .eq("id", id)
                .build();
        Optional<TemplateModel> optional = templateModelRepository.findOne(specification);
        //信息不存在
        if (!optional.isPresent()) {
            return ResultUtil.error(ResultStatusCode.MESSAGE_NOT_EXIST.getCode(), ResultStatusCode.MESSAGE_NOT_EXIST.getMsg());
        }
        templateModelRepository.deleteById(id);
        return ResultUtil.success(ResultStatusCode.DELETE_SUCCESS.getCode(),ResultStatusCode.DELETE_SUCCESS.getMsg());
    }
}
